package com.quan.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.quan.commons.core.MyBaseServiceImpl;
import com.quan.commons.utils.PageUtils;
import com.quan.site.commons.vo.SiteEnterpriseVo;
import com.quan.site.entity.SiteEnterprise;
import com.quan.site.mapper.SiteEnterpriseMapper;
import com.quan.site.service.SiteEnterpriseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SiteEnterpriseServiceImpl extends MyBaseServiceImpl<SiteEnterpriseMapper, SiteEnterprise> implements SiteEnterpriseService {

    @Autowired
    private SiteEnterpriseMapper mapper;

    @Override
    public List<SiteEnterpriseVo> queryList(PageUtils pageUtils, SiteEnterpriseVo vo) {
        QueryWrapper<SiteEnterprise> queryWrapper = new QueryWrapper<SiteEnterprise>(vo);

        List<SiteEnterprise> list = super.list(pageUtils, queryWrapper);
        List<SiteEnterpriseVo> collect = list.stream().map(item -> {
            SiteEnterpriseVo bean = new SiteEnterpriseVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        final Map<Integer, List<SiteEnterpriseVo>> listMapGroupMap = collect.stream().map(item -> {
            SiteEnterpriseVo bean = new SiteEnterpriseVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.groupingBy(SiteEnterprise::getCate));


        return collect;
    }

    @Override
    public Map<Integer, List<SiteEnterpriseVo>> listMapGroupMap(PageUtils pageUtils, SiteEnterpriseVo vo) {
        QueryWrapper<SiteEnterprise> queryWrapper = new QueryWrapper<SiteEnterprise>(vo);

        List<SiteEnterprise> list = super.list(pageUtils, queryWrapper);
        final Map<Integer, List<SiteEnterpriseVo>> listMapGroupMap = list.stream().map(item -> {
            SiteEnterpriseVo bean = new SiteEnterpriseVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.groupingBy(SiteEnterprise::getCate));

        System.out.println(listMapGroupMap);


        return listMapGroupMap;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, SiteEnterpriseVo vo) {
        QueryWrapper<SiteEnterprise> queryWrapper = new QueryWrapper<SiteEnterprise>(vo);

        if (StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<SiteEnterprise> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<SiteEnterprise> records = page.getRecords();

        final List<SiteEnterprise> collect = records.stream().map((item) -> {
            SiteEnterpriseVo bean = new SiteEnterpriseVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }
}